package libaryinterface.admin;

import libaryinterface.interfaceutils.MyDialogDemo;
import pojo.Book;
import pojo.Place;
import service.BookService;
import service.PlaceService;
import service.impl.BookServiceImpl;
import service.impl.PlaceServiceImpl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: YKH
 * Date: 2022-05-15
 * Time: 19:11
 * author:UpdateInterface
 */
public class AdminUpdateInterface extends JFrame {
    public Long id;
    public AdminUpdateInterface(Object[][] rowdata, int index) {

        //设置窗口
        super("修改界面");
        Container container = this.getContentPane();
        ImageIcon bg = new ImageIcon("src//picture//asdf.jpg");
        JLabel label = new JLabel(bg);
        label.setSize(bg.getIconWidth(),bg.getIconHeight());
        AdminUpdateInterface.super.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
        //2.把窗口面板设为内容面板并设为透明、流动布局。
        JPanel p1=(JPanel)AdminUpdateInterface.super.getContentPane();
        p1.setOpaque(false);
        p1.setLayout((new FlowLayout()));
        //设置标签字体
        Font font = new Font("楷体", Font.BOLD, 20);

        //创建标签
        JLabel label1 = new JLabel("SNID:");
        label1.setFont(font );
        label1.setBounds(80,100,100,40);
        label1.setForeground(Color.white);
        JLabel label2 = new JLabel("书名:");
        label2.setFont(font );
        label2.setBounds(80,150,100,40);
        label2.setForeground(Color.white);
        JLabel label3 = new JLabel("作者:");
        label3.setFont(font );
        label3.setBounds(80,200,100,40);
        label3.setForeground(Color.white);
        JLabel label4 = new JLabel("出版社:");
        label4.setFont(font );
        label4.setBounds(80,250,100,40);
        label4.setForeground(Color.white);
        JLabel label5 = new JLabel("剩余数量:");
        label5.setFont(font );
        label5.setBounds(80,300,100,40);
        label5.setForeground(Color.white);
        JLabel label6 = new JLabel("借出数量:");
        label6.setFont(font );
        label6.setBounds(80,350,100,40);
        label6.setForeground(Color.white);
        JLabel label7 = new JLabel("总数:");
        label7.setFont(font );
        label7.setBounds(80,400,100,40);
        label7.setForeground(Color.white);
        JLabel label8 = new JLabel("存放地点:");
        label8.setFont(font );
        label8.setBounds(80,450,100,40);
        label8.setForeground(Color.white);
        container.add(label1);
        container.add(label2);
        container.add(label3);
        container.add(label4);
        container.add(label5);
        container.add(label6);
        container.add(label7);
        container.add(label8);

        //创建文本框
        JTextField textField1 = new JTextField();
        //将选中行现有的值放入文本框中 , 下同
        textField1.setText(rowdata[index][0].toString());
        textField1.setBounds(180,100,300,40);
        textField1.setFont(font);

        JTextField textField2 = new JTextField();
        textField2.setText(rowdata[index][1].toString());
        textField2.setBounds(180,150,300,40);
        textField2.setFont(font);

        JTextField textField3 = new JTextField();
        textField3.setText(rowdata[index][2].toString());
        textField3.setBounds(180,200,300,40);
        textField3.setFont(font);

        JTextField textField4 = new JTextField();
        textField4.setText(rowdata[index][3].toString());
        textField4.setBounds(180,250,300,40);
        textField4.setFont(font);

        JTextField textField5 = new JTextField();
        textField5.setText(rowdata[index][4].toString());
        textField5.setBounds(180,300,300,40);
        textField5.setFont(font);

        JTextField textField6 = new JTextField();
        textField6.setText(rowdata[index][5].toString());
        textField6.setBounds(180,350,300,40);
        textField6.setFont(font);

        JTextField textField7 = new JTextField();
        textField7.setText(rowdata[index][6].toString());
        textField7.setBounds(180,400,300,40);
        textField7.setFont(font);
        String[] place = {"A区","B区","C区","D区"};
        JComboBox comboBox= new JComboBox(place);
        comboBox.getSelectedItem().toString();
        comboBox.setBounds(180,450,300,40);
        comboBox.setFont(font);

        container.add(textField1);
        container.add(textField2);
        container.add(textField3);
        container.add(textField4);
        container.add(textField5);
        container.add(textField6);
        container.add(textField7);
        container.add(comboBox);

        //创建按钮
        JButton button = new JButton("保存");
        button.setBounds(250,550,80,30);
        button.setFont(font);

        //this.id = (Long) rowdata[index][0];
        BookService bookService = new BookServiceImpl();
        //设置监听
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                boolean flag;
                Long rawBookId = (Long) rowdata[index][0];
                Long snid = Long.valueOf(textField1.getText());
                String name = textField2.getText();
                String author = textField3.getText();
                String press = textField4.getText();
                Long surplusNumber = Long.valueOf(textField5.getText());
                Long lendNumber = Long.valueOf(textField6.getText());
                Long allNumber = Long.valueOf(textField7.getText());
                String storagePlace = comboBox.getSelectedItem().toString();

                PlaceService placeService = new PlaceServiceImpl();
                Place place = placeService.getPlaceByName(storagePlace);
                Long placeId = place.getId();

                Book newBook = new Book(snid, name, author, press,surplusNumber,lendNumber,allNumber,placeId);
                flag = bookService.modifyBook(newBook,rawBookId);

                if( flag) {
                    new MyDialogDemo("修改成功！");
                } else {
                    new MyDialogDemo("修改失败");
                }

            }
        });
        //添加
        container.add(button);
        //获得容器
        container.setBackground(new Color(50, 144, 236));
        setBounds(600,250,600,700);
        setLayout(null);
        setVisible(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    }
}
